# -*- coding:utf-8 -*-
# File Name:delete_first_running.py
# Author:jing
# Date:2018/12/18

__author__ = 'jing'

import sys

sys.path.append('../src')
import requests
from ygframe.scope.scope import Scope

from api.v1.resource import Resource
from config import LOCAL_HOST, PORT, MONGO_HOST


def delete():
    from ygframe.mongo_wrapper import mongo_initializer
    mongo_initializer.init(MONGO_HOST)

    sample = Resource.__delete_request_sample__[0][0]
    from model.mongo_model.resource_info import ResourceInfo
    uid = sample['uid']
    from model.mongo_model.resource_info import Status
    ri = ResourceInfo.query().filter(
        ResourceInfo.user_id == uid,
        ResourceInfo.status == Status.RUNNING
    ).first().get()

    if not ri or not isinstance(ri, ResourceInfo):
        print('no resource info of {}'.format(uid))
        return

    sample['resource_id'] = ri.resource_id

    requests.delete('http://{}:{}/{}/{}/api/v1/resource'.format(
        LOCAL_HOST, PORT,
        Scope.get_system_name(), Scope.get_subsystem_name()
    ), json=sample)


if __name__ == '__main__':
    delete()
